/**
 * Created by Administrator on 2016/5/21.
 */
$(document).ready(function(){
    showInfo();
    $('.main-menu ul .openable').click(function(){
        $('.main-menu ul .openable .submenu').css('display', 'block');
        $(this).find('ul').css('display', 'block');
    });
    $('.main-menu ul .showCurrent').click(function(){
        $('.main-menu ul .openable .submenu').css('display', 'none');
    });
    modifyMovieInfo();
    updateImage();
    //updateImageConfirm();
    addMovieInfo();
    search();
    //noinspection JSJQueryEfficiency
    $("#confirmUpdate").click(function(){
        $("#updateImageForm").submit();
    });
    confirmModifyMovieInfo();
});
function fillModifyMovie(obj){
    var $modal = $("#modifyMovie");
    $modal.find("input[name=movieId]").val(obj.movieId);
    $modal.find("input[name=movieTitle]").val(obj.movieTitle);
    $modal.find("input[name=detail]").val(obj.detail);
    $modal.find("input[name=actor]").val(obj.actor);
    $modal.find("input[name=url]").val(obj.url);
    $modal.find("input[type=checkbox]").prop("checked","");
    obj.action==1? $modal.find("input[name=action]").prop("checked","checked"):"";
    obj.adventure == 1? $modal.find("input[name=adventure]").prop("checked","checked"):"";
    obj.animation==1? $modal.find("input[name=animation]").prop("checked","checked"):"";
    obj.children==1? $modal.find("input[name=children]").prop("checked","checked"):"";
    obj.comedy==1? $modal.find("input[name=comedy]").prop("checked","checked"):"";
    obj.crime==1? $modal.find("input[name=crime]").prop("checked","checked"):"";
    obj.fantasy==1? $modal.find("input[name=fantasy]").prop("checked","checked"):"";
    obj.romance==1? $modal.find("input[name=romance]").prop("checked","checked"):"";
}

function showInfo(){
    $.ajax({
        url: "/admin/movie/getAllMovies",
        type:'post',
        success:function(obj){
            var tbody = $("#movieList"),trs,
                arrays = {
                "action":"动作片",
                "adventure":"冒险",
                "animation":"动画片",
                "children":"儿童",
                "comedy":"喜剧",
                "crime":"犯罪",
                "fantasy":"玄幻",
                "romance":"爱情"
                },
                text;
            $.each(obj,function(n,value) {
                text = "";
                //debugger;
                $.each(value,function(key,value){
                    value==1 && arrays[key]? text+=arrays[key]+" ":"";
                });

                trs = $(" <tr> " +
                " <td data-title = '" + value.movieTitle + " '>" + value.movieTitle + "</td> " +
                " <td> " + new Date(value.releaseDate).Format("yyyy-MM-dd") + "</td> " +
                " <td data-title = '" + value.url + " '> <a href='" + value.url + "?rew '> 观看地址 </a> </td> " +
                " <td style='white-space:nowrap; overflow:hidden; text-overflow:ellipsis;' title="+ text +"> " + text  + "</td> " +
                " <td> <a  href='javaScript:;' class='modify' data-type='modify' data-id=' " + value.movieId + " '><u>修改</u></a> " +
                "&nbsp;&nbsp;&nbsp; <a href='javaScript:;' class='update' data-type='modify' data-id=' " + value.movieId + " '><u>编辑图片</u></a> " +
                "</td>" +
                "</tr> ");
                tbody.append(trs);
                debugger;
                trs.find("a.modify").click(function(){
                    $("#modifyMovie").modal("show");
                    fillModifyMovie(value);
                });
                trs.find("a.update").click(function(){
                    if(null != value.image){
                        var imgMes = "<img src=\" " + value.image + " \" style='width:200px;height:200px;' /> ";
                        $("#imageForm").append(imgMes);
                    }
                    $("#updateImage").modal("show");
                })
            });

        }
    });
}
function modifyMovieInfo(){
    //noinspection JSJQueryEfficiency
    //$("#movieList").on("click","a .modify",function(){
    //    alert('fdf');
    //    var id = $(this).data('id');
    //    var title = $(this).data('title');
    //    var url = $(this).data('');
    //    $("#modifyName").val(id);
    //});
}
function confirmModifyMovieInfo(){
    $("#confirmModifyMovie").click(function(){
        $.ajax({
            url: "/admin/movie/updateMovie",
            type:'post',
            data:$('#modifyMovieForm').serialize(),
            success:function(value){
                if(value == 1){
                    alert('修改成功');
                    $("#modifyMovie").modal("hide");
                    //noinspection JSJQueryEfficiency
                    $("#movieList tr").empty();
                    showInfo();
                }else{
                    alert('修改失败');
                }
            }
        });
    });
}
function updateImage(){
    $("#movieList").on("click",".update",function(){
        var id = $(this).data('id');
        $("#imageMovieId").val(id);
    });
}

//        function updateImageConfirm(){
//            $("#confirmUpdate").click(function(){
//
//            });
//        }
function addMovieInfo(){
    $("#confirmAdd").click(function(){
        //noinspection JSJQueryEfficiency
        if ("" == $("#movieTitle").val() || $("#movieTitle").val() == undefined) {
            alert('请输入电影名称！');
            return false;
        }
        //noinspection JSJQueryEfficiency
        if("" == $("#detail").val() || $("#detail").val() == undefined){
            alert('请输入影片详情');
            return false;
        }
        //noinspection JSJQueryEfficiency
        if("" == $("#actor").val() || $("#actor").val() == undefined){
            alert('请输入影片主演');
            return false;
        }
        //noinspection JSJQueryEfficiency
        if("" == $("#url").val() || $("#url").val() == undefined){
            alert('请输入url路径');
            return false;
        }
        $.ajax({
            url: "/admin/movie/addMovie",
            type:'post',
            data:$('#addMovieForm').serialize(),
            success:function(data){
                if(data == "1"){
                    alert("添加电影信息成功");
                    $('#addMovie').modal('hide');
                    //noinspection JSJQueryEfficiency
                    $("#movieList tr").empty();
                    showInfo();
                }else{
                    alert("添加电影信息失败");
                }
            }
        });
    });
}

function search(){
    $("#searchMovieBtn").click(function(){
        $.ajax({
            url: "/admin/movie/searchMovie",
            type:'post',
            data:$('#searchMovieItem').serialize(),
            success:function(obj){
                //noinspection JSJQueryEfficiency
                var tbody = $("#movieList").empty(),trs,
                    arrays = {
                        "action":"动作片",
                        "adventure":"冒险",
                        "animation":"动画片",
                        "children":"儿童",
                        "comedy":"喜剧",
                        "crime":"犯罪",
                        "fantasy":"玄幻",
                        "romance":"爱情"
                    },
                    text;
                $.each(obj,function(n,value) {
                    text = "";
                    //debugger;
                    $.each(value,function(key,value){
                        value==1 && arrays[key]? text+=arrays[key]+" ":"";
                    });

                    trs = $( " <tr> " +
                    " <td data-title = '" + value.movieTitle + " '>" + value.movieTitle + "</td> " +
                    " <td> " + new Date(value.releaseDate).Format("yyyy-MM-dd") + "</td> " +
                    " <td data-title = '" + value.url + " '> <a href='" + value.url + "?rew '> 观看地址 </a> </td> " +
                    " <td style='white-space:nowrap; overflow:hidden; text-overflow:ellipsis;' title="+ text +"> " + text  + "</td> " +
                    " <td> <a  href='javaScript:;' class='modify' data-type='modify' data-id=' " + value.movieId + " '><u>修改</u></a> " +
                    "&nbsp;&nbsp;&nbsp; <a data-toggle='modal' class='update' href='#updateImage' data-type='modify' data-id=' " + value.movieId + " '><u>编辑图片</u>></a> " +
                    "</td>" +
                    "</tr> ");
                    tbody.append(trs);
                    trs.find("a.modify").click(function(){
                        $("#modifyMovie").modal("show");
                        fillModifyMovie(value);
                    });
                    trs.find("a.update").click(function(){
                        if(null != value.image){
                            var imgMes = "<img src=\" " + value.image + " \" style='width:200px;height:200px;' /> ";
                            $("#imageForm").append(imgMes);
                        }
                        $("#updateImage").modal("show");
                    });
                });
                //noinspection JSJQueryEfficiency
                $("#movieList").append(tbody);
            }
        });
    });
}

